SWEP.Author = "Brwarner";
SWEP.Contact = "";
SWEP.Purpose = "Allows hacking of walls and other structures";
SWEP.Instructions = "Hold left click to Hack";

SWEP.Spawnable = true;
SWEP.AdminSpawnable = true;

SWEP.ViewModel = "models/weapons/v_crowbar.mdl";
SWEP.WorldModel = "models/weapons/w_crowbar.mdl";

SWEP.Primary.ClipSize = -1;
SWEP.Primary.DefaultClip = -1;
SWEP.Primary.Automatic = true;
SWEP.Primary.Ammo = "none";

SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";

function SWEP:OnRemove()
	if(timer.IsTimer("EndFire")) then timer.Destroy("EndFire"); end
end

function SWEP:PrimaryAttack()
	if(!SERVER) then return; end
	
	local tr = self.Owner:GetEyeTrace();
	local length = tr.HitPos - tr.StartPos;
	local lengthNum = length:Length();
	
	if(lengthNum < 50 && tr.Entity:GetClass() == "bh_block") then
		if(tr.Entity:GetNetworkedBool("isHack")) then return; end
		local maxHack = tr.Entity:GetHackTime();
		if(maxHack == 0) then return; end
		if(tr.Entity:GetTeam() == self.Owner:Team()) then return; end
		self:SetNetworkedFloat("hackMax", maxHack);
		local hackProgress = self:GetNetworkedFloat("hackProgress") + 0.1;
		self:SetNetworkedFloat("hackProgress", hackProgress);
		local nextfire = 0.1;
		
		//Create a spark effect
		local e = EffectData();
		e:SetStart(tr.HitPos);
		e:SetOrigin(tr.HitPos);
		e:SetScale(1);
		util.Effect("ManhackSparks", e)
		if(hackProgress >= maxHack) then
			tr.Entity:Hack(self.Weapon.Owner);
			self:SetNetworkedFloat("hackProgress", 0);
			nextfire = 0.3;
		end
		
		local this = self;
		timer.Destroy("EndFire");
		timer.Create("EndFire", 0.2, 1, function() this:ResetHack() end);
		self.Weapon:SetNextPrimaryFire( CurTime() + nextfire);
	end
end

function SWEP:ResetHack()
	self:SetNetworkedFloat("hackProgress", 0);
end

function SWEP:Initialize()
	self:SetNetworkedFloat("hackProgress", 0);
	self:SetNetworkedFloat("hackMax", 1);
end